<?php

if (!function_exists('curl_init')) {
    throw new Exception('Openid needs the CURL PHP extension.');
}

class Api {

    private $_config = array('url' => '', 'code' => '', 'key' => '');

    protected function config($key, $value) {
        $this->_config[$key] = $value;
        return $this;
    }

    protected function get_api_url($path) {
        return $this->_config['url'] . '/' . $path;
    }

    protected function get_api_key() {
        return $this->_config['key'];
    }
    
    protected function get_api_code(){
        return $this->_config['code'];
    }

    protected function get_api_config($name) {
        if (isset($this->_config[$name])) {
            return $this->_config[$name];
        }
        return null;
    }

    protected function _api($path, $security_key, $data = null) {
        $data['security_key'] = $security_key;
        $ch = curl_init($this->get_api_url($path));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

}

?>
